﻿using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CodeManDesktopBlazor.BlazorComponents.Data
{
    public class SaveConfigurationExt
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="rootname">多级用冒号:隔开</param>
        public static void SaveConfiguration(object configuration, string rootname)
        {
            var props = DictionaryFromType(configuration);
            foreach (var prop in props)
            {
                SetAppSettingValue(prop.Key, prop.Value.ToString(), rootname);
            }

        }

        private static Dictionary<string, object> DictionaryFromType(object atype)
        {
            if (atype == null) return new Dictionary<string, object>();
            Type t = atype.GetType();
            PropertyInfo[] props = t.GetProperties();
            Dictionary<string, object> dict = new Dictionary<string, object>(); // reflection
            foreach (PropertyInfo prp in props)
            {
                object value = prp.GetValue(atype, new object[] { });
                dict.Add(prp.Name, value);
            }
            return dict;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="root">多级用冒号:隔开</param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="appSettingsJsonFilePath"></param>
        public static void SetAppSettingValue(string key, string value, string root = "", string appSettingsJsonFilePath = null)
        {
            if (appSettingsJsonFilePath == null)
            {
                appSettingsJsonFilePath = System.IO.Path.Combine(System.AppContext.BaseDirectory, "appsettings.json");
            }

            var json = System.IO.File.ReadAllText(appSettingsJsonFilePath, Encoding.UTF8);
            dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JToken>(json);

            var rootArr = root.Split(':');
            switch (rootArr.Length)
            {
                case 0:
                    jsonObj[key] = value;
                    break;
                case 1:
                    jsonObj[rootArr[0]][key] = value;
                    break;
                case 2:
                    jsonObj[rootArr[0]][rootArr[1]][key] = value;
                    break;
                case 3:
                    jsonObj[rootArr[0]][rootArr[1]][rootArr[2]][key] = value;
                    break;
                case 4:
                    jsonObj[rootArr[0]][rootArr[1]][rootArr[2]][rootArr[3]][key] = value;
                    break;
                case 5:
                    jsonObj[rootArr[0]][rootArr[1]][rootArr[2]][rootArr[3]][rootArr[4]][key] = value;
                    break;
                default:
                    jsonObj[root][key] = value;
                    break;
            }
            string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Newtonsoft.Json.Formatting.Indented);

            System.IO.File.WriteAllText(appSettingsJsonFilePath, output, Encoding.UTF8);
        }
    }
}
